Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-initialize the cache if the directory was deleted. #297

Merged
merged 3 commits into from
Oct 3, 2019

Conversation

uhager
Copy link
Contributor

@uhager uhager commented Oct 2, 2019

If the user clears the cache from the Android Settings page, the
DiskBasedCache's root directory is deleted. This means that the app will
be running without a cache until it is restarted and the cache
initialized.
This fix initializes the cache when it finds that the root directory no
longer exists.
Note that the first entry after deletion that is put into the cache is
still lost, the cache is only re-initialized when putting that entry
fails. Adding retries would be more complicated, since we would have to
avoid getting into a loop if creating the root directory fails.

If the user clears the cache from the Android Settings page, the
DiskBasedCache's root directory is deleted. This means that the app will
be running without a cache until it is restarted and the cache
initialized.
This fix initializes the cache when it finds that the root directory no
longer exists.
Note that the first entry after deletion that is put into the cache is
still lost, the cache is only re-initialized when putting that entry
fails. Adding retries would be more complicated, since we would have to
avoid getting into a loop if creating the root directory fails.
@Test
public void initializeIfRootDirectoryDeleted() {
temporaryFolder.delete();
DiskBasedCache uninitializedCache = new DiskBasedCache(temporaryFolder.getRoot(), MAX_SIZE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - in the spirit of testing more representative behavior, should this unit test perhaps create the cache, call initialize(), and only then delete the temporary folder?

The behavior tested here isn't a supported sequence in general - we don't expect the cache to work if you don't initialize it. It happens to hit the code path you want, but that's not necessarily reliable. On the other hand, we know user devices hit the scenario that an already-initialized cache has the directory deleted from underneath it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Rather than testing with an uninitialized test, the test should use an
initialized cache, delete the root directory, and then confirm that the
cache is re-initialized.
@@ -258,10 +258,11 @@ public synchronized void put(String key, Entry entry) {
pruneIfNeeded();
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return;

If move logic inside the catch block, then remove the return that is no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

After the other code was moved into the catch statement, this return is
no longer needed.
@jpd236 jpd236 merged commit 514eac8 into google:master Oct 3, 2019
@jpd236
Copy link
Collaborator

jpd236 commented Oct 3, 2019

Thanks for the change and the review!

HeterPu pushed a commit to HeterPu/volley that referenced this pull request Oct 7, 2019
Re-initialize the cache if the directory was deleted. (google#297)
HeterPu added a commit to HeterPu/volley that referenced this pull request Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants